package com.foreknow.dao.impl;

import com.foreknow.dao.BusinessDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.model.Business;

import java.sql.SQLException;
import java.util.List;

public class BusinessDaoImpl extends BaseDAO implements BusinessDao {
    @Override
    public boolean save(Business business) throws SQLException {
        String sql = "insert into business(businessName,password,businessAddress,businessExplain,starPrice,deliveryPrice) values(?,?,?,?,?,?)";
        int i =jdbcTemplate.update(sql,business.getBusinessName(),business.getBusinessPassword(),business.getBusinessAddress(),business.getBusinessExplain(),business.getStarPrice(),business.getDeliveryPrice());
        return i==1;
    }

    @Override
    public List<Object> getQueryAll() throws SQLException {
        EntityMapping mapping = mappingFactory.getMap("businessmapping");
        List<Object> list = jdbcTemplate.query("select * from business",mapping);
        return  list;
    }

    @Override
    public int Update(Business business, int id) throws SQLException {
        System.out.println(business.getBusinessPassword());
        String sql = "update business set businessName=?,password=?,businessAddress=?,businessExplain=?,starPrice=?,deliveryPrice=? where businessId=?";
        int i =jdbcTemplate.update(sql,business.getBusinessName(),business.getBusinessPassword(),business.getBusinessAddress(),business.getBusinessExplain(),business.getStarPrice(),business.getDeliveryPrice(),id);
        return i;

    }

    @Override
    public List<Object> getQueryByName(String businessName) throws SQLException {
        String sql = "select * from business where businessName like concat('%',?,'%')";
        EntityMapping mapping = mappingFactory.getMap("businessmapping");
         List<Object> a = jdbcTemplate.query(sql,mapping,businessName);
         return a;

    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        String sql = "delete from business where businessId=?";
        int i = jdbcTemplate.update(sql,id);
        return i==1;
    }
}
